﻿using System;
using System.Diagnostics;
using Reflector.CodeModel;

namespace CallGraphExtractor.CecilAdapter
{
    [DebuggerDisplay("Name")]
    abstract class MemberDeclaration : IMemberReference, IMemberDeclaration
    {
        #region Lifecycle

        protected MemberDeclaration(Mono.Cecil.MemberReference memberReference)
        {
            this.Adaptee = memberReference;
        }

        #endregion Lifecycle

        #region Properties

        protected Mono.Cecil.MemberReference Adaptee { get; private set; }

        #endregion Properties

        #region Methods

        public override string ToString()
        {
            string[] tokens = this.Adaptee.FullName.Split(' ');
            tokens[0] = "";
            string s = string.Concat(tokens);//.;
            if (this.Adaptee.DeclaringType.IsNested == false)
                s = s.Replace(this.Adaptee.DeclaringType.Namespace + ".", "");
            else
            {
                string fn = this.Adaptee.DeclaringType.FullName;
                string n = this.Adaptee.DeclaringType.Name;
                string toRemove = fn.Replace(n, "");
                s = s.Replace(toRemove, "");
            }
            return s.Replace("::", ".").Replace('/', '.');
        }

        #endregion Methods

        #region IMemberReference Members

        public IType DeclaringType
        {
            get { return new TypeDeclaration(this.Adaptee.DeclaringType.Resolve()); }
            set { throw new NotSupportedException(); }
        }

        public string Name
        {
            get { return this.Adaptee.Name; }
            set { throw new NotSupportedException(); }
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object obj)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ICustomAttributeProvider Members

        public ICustomAttributeCollection Attributes
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IDocumentationProvider Members

        public string Documentation
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
    }
}
